package com.zbkj.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zbkj.client.config.ClientTokenComponent;
import com.zbkj.client.service.ClientLoginService;
import com.zbkj.common.model.client.agent.PackageConfig;
import com.zbkj.common.model.client.agent.UserLocation;
import com.zbkj.common.model.client.agent.WeekTask;
import com.zbkj.common.request.ClientUpdatePasswordRequest;
import com.zbkj.common.response.ClientLoginResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.utils.OssUtil;
import com.zbkj.service.service.PackageConfigService;
import com.zbkj.service.service.SystemAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/client/package")
@Api(tags = "套餐控制器")
public class PackageController {

    @Autowired
    private SystemAdminService systemAdminService;

    @Autowired
    private ClientTokenComponent clientTokenComponent;

    @Autowired
    private ClientLoginService clientLoginService;

    @Autowired
    private PackageConfigService packageConfigService;

    @ApiOperation(value = "修改套餐配置")
    @RequestMapping(value = "/updatePackage", method = RequestMethod.POST)
    public CommonResult<String> updatePackage(@RequestBody Map<String, Object> params,
                                                   HttpServletRequest httpRequest) {

        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setId((Integer) params.get("id"));

        // 保序处理：将 configList 转为 LinkedHashMap 以保留前端传入的键顺序
        Object rawConfigList = params.get("configList");
        if (rawConfigList != null) {
            ObjectMapper mapper = new ObjectMapper();
            LinkedHashMap<String, LinkedHashMap<String, Object>> orderedConfig =
                    mapper.convertValue(rawConfigList, new TypeReference<LinkedHashMap<String, LinkedHashMap<String, Object>>>() {});
            // 按声明类型赋值，底层为 LinkedHashMap，Jackson 序列化时会按插入顺序输出
            packageConfig.setConfigList((Map) orderedConfig);
        }

        if (params.get("tags") != null) {
            packageConfig.setTag((List<String>) params.get("tags"));
        }

        packageConfigService.updateById(packageConfig);
        return CommonResult.success("修改成功");
    }

    /**
     * 获取套餐配置列表
     * @param httpRequest
     * @return
     */
    @GetMapping("/getConfigList")
    public CommonResult<List<PackageConfig>> getConfigList(HttpServletRequest httpRequest) {

        QueryWrapper<PackageConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del", 0)
                .orderByAsc("package_level");

        List<PackageConfig> res = packageConfigService.list(wrapper);

        return CommonResult.success(res);
    }

    /**
     * 获取配置详情
     * @param httpRequest
     * @return
     */
    @GetMapping("/getConfigDetail")
    public CommonResult<PackageConfig> getConfigDetail(@RequestParam Integer id,  HttpServletRequest httpRequest) {

        PackageConfig packageConfig = packageConfigService.lambdaQuery()
                .eq(PackageConfig::getIsDel, 0)
                .eq(PackageConfig::getPackageLevel, id)
                .one();

        if (packageConfig != null) {
            return CommonResult.success(packageConfig);
        } else {
            return CommonResult.failed("未找到对应的配置信息");
        }
    }

    /**
     * 获取配置详情
     * @param httpRequest
     * @return
     */
    @GetMapping("/test")
    public CommonResult<?> test(HttpServletRequest httpRequest) {

        String folder = "client/3";

        long l = OssUtil.calculateFolderSize(folder);

        return CommonResult.success(l);
    }
} 